#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
LL fa[maxn];
LL find(LL x){
    if(x!=fa[x]) return fa[x]=find(fa[x]);
    else return fa[x];
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL n,m;cin>>n>>m;
    LL sum=0;
    for(LL i=1;i<=n;i++) fa[i]=i;
    LL res=0;
    while(m--){
        LL x,y;cin>>x>>y;
        if(x==y) continue;
        if(find(x)!=find(y)){
            fa[find(x)]=find(y);
        }
        else{
            res++;
        }
        sum++;
    }
    cout<<sum+res<<endl;
  }
return 0;
}
